package basic.lessons.mosaic;

import java.awt.*;

public class RandomMosaicWalkRedBlack {

	/*
	 * In this applet, all the squares start out with the default color, black.
	 * Every time the disturbance visits a square, a small amount is added to
	 * the red component of the color of that square. Write a subroutine that
	 * will add 25 to the red component of one of the squares in the mosaic. The
	 * row and column numbers of the square should be passed as parameters to
	 * the subroutine. Recall that you can discover the current red component of
	 * the square in row r and column c with the function call
	 * Mosaic.getRed(r,c)
	 */
	   
	   final static int ROWS = 30;        // Number of rows in the mosaic.
	   final static int COLUMNS = 30;     // Number of columns in the mosaic.
	   final static int SQUARE_SIZE = 15; // Size of each square in the mosaic.
	       
	   static int currentRow; // row currently containing the disturbance
	   static int currentColumn; // column currently containing disturbance
	           
	   public static void main(String[] args) {
	          // Main program creates the window, fills it with
	          // random colors, then moves the disturbance in
	          // a random walk around the window.
	       Mosaic.open(ROWS, COLUMNS, SQUARE_SIZE, SQUARE_SIZE);
	       Mosaic.fill(Color.black);
	       currentRow = ROWS / 2;   // start at center of window
	       currentColumn = COLUMNS / 2;
	       while (Mosaic.isOpen()) {
	    	   addRedToColor(currentRow, currentColumn);
	           randomMove();
	           Mosaic.delay(20);
	       }
	   }  // end of main()
	   
	   static void fillWithRandomColors() {
	           // Precondition:  The mosaic window is open.
	           // Postcondition: Each rectangles have been set to a
	           //                random color
	        for (int row=0; row < ROWS; row++) {
	           for (int column=0; column < COLUMNS; column++) {
	        	   addRedToColor(row, column);  
	           }
	        }
	   }  // end of fillWithRandomColors()
	   
	   
	   static void addRedToColor(int rowNum, int colNum) {
	           // Precondition:  rowNum and colNum are in the valid range
	           //                of row and column numbers.
	           // Postcondition: The rectangle in the specified row and
	           //                column has been changed to a random color.
	        int red = Mosaic.getRed(rowNum, colNum)+25;    // choose random levels in range
	        int green = 0;  //     0 to 255 for red, green, 
	        int blue = 0;   //     and blue color components

	        Mosaic.setColor(rowNum,colNum,red,green,blue);  
	    }  // end of changeToRandomColor()
	    
	    static void randomMove() {
	           // Precondition:  The global variables currentRow and currentColumn
	           //                specify a valid position in the grid.
	           // Postcondition: currentRow or currentColumn is changed to
	           //                one of the neighboring positions in the grid,
	           //                up, down, left, or right from the previous
	           //                position.  (If this moves the position outside
	           //                the grid, then it is moved to the opposite edge
	           //                of the window.
	        int directionNum; // Randomly set to 0, 1, 2, or 3 to choose direction.
	        directionNum = (int)(4*Math.random());
	        switch (directionNum) {
	           case 0:  // move up 
	              currentRow--;
	              if (currentRow < 0)
	                 currentRow = ROWS - 1;
	              break;
	           case 1:  // move right
	              currentColumn++;
	              if (currentColumn >= COLUMNS)
	                 currentColumn = 0;
	              break; 
	           case 2:  // move down
	              currentRow ++;
	              if (currentRow >= ROWS)
	                 currentRow = 0;
	              break;
	           case 3:  
	              currentColumn--;
	              if (currentColumn < 0)
	                 currentColumn = COLUMNS - 1;
	              break; 
	        }
	    }  // end of randomMove()
	   
	} // end of class RandomMosaicWalk2